package com.andova.component.lbs;

import android.view.View;

import com.andova.component.AndovaComponentManager;
import com.baidu.location.BDLocation;
import com.baidu.location.LocationClientOption;
import com.baidu.location.LocationClientOption.LocationMode;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.TextureMapView;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.model.LatLngBounds;

import java.util.List;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

/**
 * Created by Administrator on 2017-12-06.
 *
 * @author kzaxil
 * @since 1.0.1
 */
public class BaiDuMapActivity extends AppCompatActivity implements
        ILbsMapView<TextureMapView>, ILbsLatLng<LatLng>, ILbsLocMode<LocationMode> {
    @Override
    protected void onResume() {
        super.onResume();
        if (mapView() == null) return;
        mapView().onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (mapView() == null) return;
        mapView().onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mapView() == null) return;
        mapView().onDestroy();
    }

    @Override
    public TextureMapView mapView() {
        return null;
    }

    @Override
    public void marker(List<LatLng> lat_lng, View marker) {
        marker(lat_lng, marker, null, null);
    }

    public void marker(List<LatLng> lat_lng, View marker, BaiduMap.OnMarkerClickListener listener, MapStatus mapStatus) {
        if (mapView() == null) return;
        LatLngBounds.Builder builder = new LatLngBounds.Builder();
        for (LatLng latLng : lat_lng) {
            LatLngBounds bounds = builder.build();
            mapView().getMap().setMapStatus(MapStatusUpdateFactory.newLatLngBounds(bounds));
            MarkerOptions markerOption = new MarkerOptions();
            markerOption.anchor(0.5f, 1.0f);
            markerOption.position(latLng);
            markerOption.icon(BitmapDescriptorFactory.fromView(marker));
            mapView().getMap().addOverlay(markerOption);
            builder.include(latLng);
        }
        if (listener != null) mapView().getMap().setOnMarkerClickListener(listener);
        LatLngBounds bounds = builder.build();
        mapView().getMap().setMapStatus(MapStatusUpdateFactory.newLatLngBounds(bounds));
        if (mapStatus != null) {
            mapView().getMap().setMapStatus(MapStatusUpdateFactory.newMapStatus(mapStatus));
        }
    }

    @Override
    public LocationMode locMode() {
        try {
            return ((LocationClientOption) AndovaComponentManager.getInstance()
                    .lbsComponent().lbsLocOption()
                    .locOption()).getLocationMode();
        } catch (ClassCastException | NullPointerException e) {
            e.printStackTrace();
            return null;
        }
    }

    @Nullable
    @Override
    public AndovaLbsComponent option() {
        return AndovaComponentManager.getInstance().lbsComponent();
    }

    @Override
    public String version() {
        return "5.2.1";
    }

    @Nullable
    @Override
    public String coorType() {
        try {
            return ((LocationClientOption) AndovaComponentManager.getInstance()
                    .lbsComponent().lbsLocOption()
                    .locOption()).getCoorType();
        } catch (ClassCastException | NullPointerException e) {
            e.printStackTrace();
            return "";
        }
    }

    /**
     * 获取当前所在位置信息
     */
    @Nullable
    public BDLocation locInfo() {
        AndovaLbsComponent component = AndovaComponentManager.getInstance().lbsComponent();
        if (component == null) return null;
        return (BDLocation) component.lbsLocInfo().locInfo();
    }
}
